import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'

// 布局组件
import AdminLayout from '../layout/AdminLayout.vue'
import StudentLayout from '../layout/StudentLayout.vue'

Vue.use(VueRouter)

// 路由配置
const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  // 管理端路由
  {
    path: '/admin',
    component: AdminLayout,
    redirect: '/admin/home',
    children: [
      {
        path: 'home',
        name: 'AdminHome',
        component: () => import('../views/admin/Home.vue')
      },
      {
        path: 'student',
        name: 'StudentManage',
        component: () => import('../views/admin/Home.vue')
      },
      {
        path: 'teacher',
        name: 'TeacherManage',
        component: () => import('../views/admin/Home.vue')
      },
      {
        path: 'class',
        name: 'ClassManage',
        component: () => import('../views/admin/Home.vue')
      },
      {
        path: 'college',
        name: 'CollegeManage',
        component: () => import('../views/admin/Home.vue')
      },
      {
        path: 'question',
        name: 'QuestionManage',
        component: () => import('../views/admin/Home.vue')
      },
      {
        path: 'exam',
        name: 'ExamManage',
        component: () => import('../views/admin/Home.vue')
      },
      {
        path: 'score',
        name: 'ScoreManage',
        component: () => import('../views/admin/Home.vue')
      }
    ]
  },
  // 用户端路由
  {
    path: '/student',
    component: StudentLayout,
    redirect: '/student/exam',
    children: [
      {
        path: 'exam',
        name: 'StudentExam',
        component: () => import('../views/student/Exam.vue')
      },
      {
        path: 'score',
        name: 'StudentScore',
        component: () => import('../views/student/Score.vue')
      },
      {
        path: 'class',
        name: 'StudentClass',
        component: () => import('../views/student/Class.vue')
      }
    ]
  },
  // 404页面
  {
    path: '*',
    component: () => import('../views/404.vue')
  }
]

const router = new VueRouter({
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  try {
    // 获取用户信息和token
    const token = store.getters.getToken
    const userInfo = store.getters.getUserInfo
    const userRole = store.getters.getUserRole
    
    // 白名单路径，不需要登录即可访问
    const whiteList = ['/login']
    
    // 如果在白名单中，直接放行
    if (whiteList.includes(to.path)) {
      // 如果已登录且有token，根据角色跳转到对应首页
      if (token && userInfo && userInfo.role) {
        if (userRole === 'admin' || userRole === 'teacher') {
          return next('/admin/home')
        } else if (userRole === 'student') {
          return next('/student/exam')
        }
      }
      return next()
    }
    
    // 非白名单页面，检查是否有token和用户信息
    if (!token || !userInfo || !userInfo.role) {
      // 没有token或用户信息，跳转到登录页
      return next('/login')
    }
    
    // 根据用户角色判断权限
    if (userRole === 'student' && to.path.startsWith('/admin')) {
      return next('/student/exam')
    }
    
    // 放行
    return next()
  } catch (error) {
    console.error('路由守卫错误:', error)
    return next('/login')
  }
})

export default router
